package edu.technion.cse.consolePlus;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class ConsolePlus implements ActionListener {

    private String newline = "\n";
    private JTextPane textPane = null;
    private JFrame frame = null;
    private JPanel panel = null;
	  private String currentStyle = "regular";
	
    public String getCurrentStyle() {
		return currentStyle;
	}

	public void setCurrentStyle(String currentStyle) {
		this.currentStyle = currentStyle;
	}
	
	
	public void setFontSize(int size) {
		Style st = textPane.getStyledDocument().getStyle("current");
		StyleConstants.setFontSize(st, size);
	}
	
	public void setFontColor(Color color) {
		Style st = textPane.getStyledDocument().getStyle("current");
		StyleConstants.setForeground(st, color);
	}

	protected static final String textFieldString = "JTextField";

    public ConsolePlus() {
    	
        panel = new JPanel();
        panel.setLayout(new BorderLayout());

        //Create a text pane.
        JTextPane textPane = createTextPane();
        textPane.setBackground(Color.BLACK);
        JScrollPane paneScrollPane = new JScrollPane(textPane);
        paneScrollPane.setVerticalScrollBarPolicy(
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        paneScrollPane.setPreferredSize(new Dimension(450, 405));
        paneScrollPane.setMinimumSize(new Dimension(10, 10));

        //
        panel.add(paneScrollPane);
    }

    public void actionPerformed(ActionEvent e) {
//        String prefix = "You typed \"";
//        if (textFieldString.equals(e.getActionCommand())) {
//            JTextField source = (JTextField)e.getSource();
//            actionLabel.setText(prefix + source.getText() + "\"");
//        } else if (passwordFieldString.equals(e.getActionCommand())) {
//            JPasswordField source = (JPasswordField)e.getSource();
//            actionLabel.setText(prefix + new String(source.getPassword())
//                                + "\"");
//        } else if (buttonString.equals(e.getActionCommand())) {
//            Toolkit.getDefaultToolkit().beep();
//        }
    }

    private JTextPane createTextPane() {
        String[] initString =
                { "This is an editable JTextPane, ",            //regular
                  "another ",                                   //italic
                  "styled ",                                    //bold
                  "text ",                                      //small
                  "component, ",                                //large
                  "which supports embedded components..." + newline,//regular
                  " " + newline,                                //button
                  "...and embedded icons..." + newline,         //regular
                  " ",                                          //icon
                  newline + "JTextPane is a subclass of JEditorPane that " +
                    "uses a StyledEditorKit and StyledDocument, and provides " +
                    "cover methods for interacting with those objects."
                 };

        String[] initStyles =
                { "regular", "italic", "bold", "small", "large",
                  "regular", "button", "regular", "icon",
                  "regular"
                };

        textPane = new JTextPane();
        StyledDocument doc = textPane.getStyledDocument();
        addStylesToDocument(doc);

        try {
            for (int i=0; i < initString.length; i++) {
                doc.insertString(doc.getLength(), initString[i],
                                 doc.getStyle(initStyles[i]));
            }
        } catch (BadLocationException ble) {
            System.err.println("Couldn't insert initial text into text pane.");
        }

        return textPane;
    }

    protected void addStylesToDocument(StyledDocument doc) {
        //Initialize some styles.
        Style def = StyleContext.getDefaultStyleContext().
                        getStyle(StyleContext.DEFAULT_STYLE);

        Style regular = doc.addStyle("regular", def);
        StyleConstants.setFontFamily(def, "SansSerif");
        StyleConstants.setForeground(def, Color.WHITE);
        
        Style s = doc.addStyle("current", regular);

        s = doc.addStyle("italic", regular);
        StyleConstants.setItalic(s, true);

        s = doc.addStyle("bold", regular);
        StyleConstants.setBold(s, true);

        s = doc.addStyle("small", regular);
        StyleConstants.setFontSize(s, 10);

        s = doc.addStyle("large", regular);
        StyleConstants.setFontSize(s, 16);
        
        s = doc.addStyle("red", regular);
        StyleConstants.setFontSize(s, 10);
        StyleConstants.setForeground(s, Color.RED);
        
        s = doc.addStyle("a1", regular);
        StyleConstants.setFontSize(s, 10);
        StyleConstants.setForeground(s, Color.WHITE);
        StyleConstants.setBackground(s, Color.BLACK);
        

        s = doc.addStyle("icon", regular);
        StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
        ImageIcon pigIcon = createImageIcon("images/Pig.gif",
                                            "a cute pig");
        if (pigIcon != null) {
            StyleConstants.setIcon(s, pigIcon);
        }

        s = doc.addStyle("button", regular);
        StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
//        ImageIcon soundIcon = createImageIcon("images/sound.gif",
//                                              "sound icon");
//        JButton button = new JButton();
//        if (soundIcon != null) {
//            button.setIcon(soundIcon);
//        } else {
//            button.setText("BEEP");
//        }
//        button.setCursor(Cursor.getDefaultCursor());
//        button.setMargin(new Insets(0,0,0,0));
//        //button.setActionCommand(buttonString);
//        button.addActionListener(this);
//        StyleConstants.setComponent(s, button);
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = ConsolePlus.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public void createAndShowGUI() {
        //Create and set up the window.
    	frame = new JFrame("TextSamplerDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        frame.add(panel);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

//    public static void main(String[] args) {
//        //Schedule a job for the event dispatching thread:
//        //creating and showing this application's GUI.
//        SwingUtilities.invokeLater(new Runnable() {
//            public void run() {
//                 //Turn off metal's use of bold fonts
//		UIManager.put("swing.boldMetal", Boolean.FALSE);
//		createAndShowGUI();
//            }
//        });
//        
//    }

	public void print(String str) {
		StyledDocument doc = textPane.getStyledDocument();
		try {
			Style st = doc.getStyle("current");
			doc.insertString(doc.getLength(), str,
					st);
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("textPane= " + textPane.getText());
	}

}
